<template>
  <div>
    <el-container>
      <el-header style="height: 110px">
        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 18px">
          <el-breadcrumb-item :to="{ path: '/' }" style="font-weight: bold;color: black"><i class="el-icon-s-promotion" style="margin-right: 5px"></i>后台管理</el-breadcrumb-item>
          <el-breadcrumb-item>类别管理</el-breadcrumb-item>
          <el-breadcrumb-item v-for="item in history" :key="item.id"><span v-text="item.name"></span></el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <div>
          <el-button type="primary" size="medium" @click="$router.push('/sys-admin/product/album/add-new')">添加类别</el-button>
        </div>
      </el-header>
      <el-main>
        <el-table
            :data="tableData"
            style="width: 100%"
        >
          <el-table-column
              prop="id"
              label="ID"
              width="100px"
              align="center"
          >
          </el-table-column>
          <el-table-column
              label="图标"
              width="100px"
              align="center"
          >
            <el-avatar size="small" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"></el-avatar>
          </el-table-column>
          <el-table-column
              prop="name"
              label="名称"
              show-overflow-tooltip
              align="center"
              width="160px"
          >
          </el-table-column>
          <el-table-column
              prop="keywords"
              label="关联词"
              width="550px"
              header-align="center"
          >
          </el-table-column>
          <el-table-column
              prop="sort"
              label="排序序号"
              width="120px"
              align="center"
          >
          </el-table-column>
          <el-table-column
              label="是否启用"
              width="120px"
              align="center"
          >
            <el-switch
                v-model="tableData.enable"
                active-value="1"
                inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949">
            </el-switch>
          </el-table-column>
          <el-table-column
              label="是否在导航栏"
              width="120px"
              align="center"
          >
            <el-switch
                v-model="tableData.isDisplay"
                active-value="1"
                inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949">
            </el-switch>
          </el-table-column>
          <el-table-column label="子类级别"
              width="120px"
              align="center"
          >
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="primary"
                  @click="openSon(scope.row)"
                  :disabled="scope.row.isParent==0"
              >查看</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作"
              align="center"
              width="120px"
          >
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  circle
                  icon="el-icon-edit"
                  type="success"
                  @click="handleGetStandard(scope.row)"
              ></el-button>
              <el-button
                  size="mini"
                  type="danger"
                  circle
                  icon="el-icon-delete"
                  @click="openDeleteConfirm(scope.row)"
                  :disabled="scope.row.isParent==1"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <div v-if="history.length!=0" style="float: right;margin-top: 20px;margin-right: 45px">
          <el-button @click="goback">返回</el-button>
        </div>

<!--        <el-pagination-->
<!--            style="text-align: right;margin-right: 15px;margin-top: 15px"-->
<!--            layout="total, prev, pager, next"-->
<!--            :total="total"-->
<!--            :page-size="pageSize"-->
<!--            :current-page="currentPage"-->
<!--            @current-change="changePage"-->
<!--        >-->
<!--        </el-pagination>-->
      </el-main>
    </el-container>

    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="albumStandard"  ref="ruleForm" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="albumStandard.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="albumStandard.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model="albumStandard.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateAlbum">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      // total: 0,
      // pageSize: 0,
      // currentPage: 1,
      tableData: [],
      dialogFormVisible: false,
      albumStandard: {
        id: '',
        name: '',
        description: '',
        sort: ''
      },
      // rules: {
      //   name: [
      //     {required: true, message: '请输入名称', trigger: 'blur'},
      //     {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
      //   ],
      //   description: [
      //     {required: true, message: '请输入简介', trigger: 'blur'}
      //   ],
      //   sort: [
      //     {required: true, message: '请输入排序序号', trigger: 'blur'},
      //     {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
      //   ]
      // }
      currentParentId: 0,
      history: []
    }
  },
  methods: {
    handleUpdateAlbum() {
      let url = "http://localhost:9190/album/update";
      console.log(url);

      let formData = this.qs.stringify(this.albumStandard)
      console.log(formData);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult);

        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改相册成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAlbumList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    handleGetStandard(category) {

      let url = "http://localhost:9190/category/standard?id=" + category.id;
      console.log('url=' + url);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult);

        if (jsonResult.state == 20000) {
          this.albumStandard = jsonResult.data;
          console.log("根据id查到的相册数据为：" + this.albumStandard);
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        }
      });
    },
    // changePage(value) {
    //   this.$router.replace('?pageNum=' + value);
    //   this.loadAlbumList();
    // },
    loadCategoryList() {

      let url = "http://localhost:9190/category/listCategory?id=" + this.currentParentId;
      console.log(url)

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
          // this.total = jsonResult.data.total;
          // this.pageSize = jsonResult.data.pageSize;
          // this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openDeleteConfirm(category) {
      this.$confirm('此操作将永久删除【' + category.id + '-' + category.name + '】类别，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDeleteCategory(category);
      }).catch(() => {
      });
    },
    handleDeleteCategory(category) {
      let url = "http://localhost:9190/category/delete";
      console.log('url=' + url);

      console.log("category.id=" + category.id)

      // let formData = 'id=' + album.id;

      let formData = this.qs.stringify(category);
      console.log("formData=" + formData);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadCategoryList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });

    },
    openSon(category) {
      this.history[category.depth - 1] = category;

      this.currentParentId = category.id;
      this.loadCategoryList();
    },
    goback() {
      let parentCategory = this.history[this.history.length - 1];
      this.history.pop();
      this.currentParentId = parentCategory.parentId;
      console.log(parentCategory);
      this.loadCategoryList();

    }
  },
  mounted() {
    this.loadCategoryList();
  }
}
</script>
